<?php

class Filter {
    public $attribute;
    public $operator;
    public $operand;
    
    function __construct($attribute, $operator, $operand) {
        $this->attribute = $attribute;
        $this->operator = $operator;
        $this->operand = $operand;
        
    }

    function escapeString($operand)  {
        return mysql_real_escape_string($operand);
    }
    function escape($operand) {
        if (stripos($operand, "date(") !== false) { return $operand; }
        if (stripos($operand, "now(") !== false) { return $operand; }
        return "'".$this->escapeString($operand)."'";
    }

    function toSQL($mappingRules) {
        switch ($this->operator) {
            case ">":
            case "<":
            case ">=":
            case "<=":
            case "!=":
            case "=": return $mappingRules[$this->attribute]->column ." ".$this->operator." ".$this->escape($this->operand)."";

            case "startsWith": return $mappingRules[$this->attribute]->column ." LIKE '". $this->escapeString($this->operand)."%'";
            case "endsWith": return $mappingRules[$this->attribute]->column ." LIKE '%". $this->escapeString($this->operand)."'";
            case "contains": return $mappingRules[$this->attribute]->column ." LIKE '%". $this->escapeString($this->operand)."%'";

        }
    }

}
?>
